//
//  FriendRemarkView.m
//  project
//
//  Created by 黄建华 on 2018/8/14.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "FriendRemarkView.h"

@implementation FriendRemarkView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype)initWithFrame:(CGRect)frame withModel:(FriednInviterModel *)model {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.borderWidth   = 0.5;
        self.layer.borderColor   = SNCOLORGRAYLINE.CGColor;
        self.layer.cornerRadius  = 5.0;
        self.layer.masksToBounds = YES;
        _model                   = model;
        [self initRemarkView];
    }
    return self;
}

- (void)initRemarkView {
    UILabel *labelTitle = [[UILabel alloc] init];
    
    labelTitle.frame         = SNFRAME(0, 20, self.width, 30);
    labelTitle.textAlignment = NSTextAlignmentCenter;
    labelTitle.textColor     = SNCOLORBLACK;
    labelTitle.font          = SNFONT(25.0);
    labelTitle.text          = SN_Text(@"设置备注");
    [self addSubview:labelTitle];
    
    _remarkTextfield               = [[UITextField alloc] init];
    _remarkTextfield.placeholder   = SN_Text(@"请设置备注");
    _remarkTextfield.font          = SNFONT(15.0);
    _remarkTextfield.textAlignment = NSTextAlignmentCenter;
    _remarkTextfield.textColor     = SNCOLORGRAY;
    _remarkTextfield.frame         = SNFRAME(10, labelTitle.bottom +25, self.width -20, 25);
    [self addSubview:_remarkTextfield];
    
    if ([_model.remark isEqualToString:SN_Text(@"未填写备注")]) {
        _remarkTextfield.placeholder = SN_Text(@"请设置备注");
    } else {
        _remarkTextfield.text = _model.remark;
    }
    _lastText = _remarkTextfield.text;
    
    UIView *line = [[UIView alloc] init];
    line.frame           = SNFRAME(_remarkTextfield.left, _remarkTextfield.bottom + 3, _remarkTextfield.width, 2);
    line.backgroundColor = SNCOLORGRAYLINE;
    [self addSubview:line];
    
    UIButton *cancelButton = [[UIButton alloc] init];
    [cancelButton setTitle:SN_Text(@"取消") forState:UIControlStateNormal];
    cancelButton.backgroundColor     = SNCOLORBLACK;
    cancelButton.layer.cornerRadius  = 5.0;
    cancelButton.layer.masksToBounds = YES;
    cancelButton.tag                 = 1;
    cancelButton.frame               = SNFRAME(10, self.height - 70,(self.width/2 -20), 40);
    [self addSubview:cancelButton];
    
    UIButton *okButton = [[UIButton alloc] init];
    [okButton setTitle:SN_Text(@"确定") forState:UIControlStateNormal];
    okButton.backgroundColor     = SNCOLORBLACK;
    okButton.layer.cornerRadius  = 5.0;
    okButton.layer.masksToBounds = YES;
    okButton.tag                 = 2;
    okButton.frame               = SNFRAME(self.width/2 +10, self.height - 70,cancelButton.width, 40);
    [self addSubview:okButton];
    
    labelTitle.adjustsFontSizeToFitWidth = YES;
    labelTitle.minimumFontSize = 0.5;
    _remarkTextfield.adjustsFontSizeToFitWidth = YES;
    _remarkTextfield.minimumFontSize = 0.5;
    cancelButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    cancelButton.titleLabel.minimumScaleFactor = 0.5;
    okButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    okButton.titleLabel.minimumScaleFactor = 0.5;
    
    [cancelButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [okButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClick:(UIButton *)sender {
    if (sender.tag == 1) {
        [sender dismissPopup];
    }
    if (sender.tag == 2) {
        if (_remarkTextfield.text.length == 0 && _remarkTextfield.text.length > 32) {
            SHOWMBProgressHUD(SN_Text(@"请设置备注"), nil, nil, NO, 2.0);
            return;
        }
        
        if ([_remarkTextfield.text isEqualToString:_lastText]) {
            [self dismissPopup];
            return;
        }
        
        [SN_Net friendRemarkFriend:StrByInt(_model.userId) Remark:_remarkTextfield.text Success:^(id object) {
            if (_remarkClickBlock) {
                _remarkClickBlock();
            }
        } fail:^(id object) {
        }];
        
        [self dismissPopup];
    }
}

@end
